//
//  DYZShowController.m
//  DYZCars
//
//  Created by 李凯 on 16/8/18.
//  Copyright © 2016年 MF. All rights reserved.
//

#import "DYZShowController.h"
#import "Masonry.h"
#import "DYZNewLaunchCell.h"
#import "DYZCarShowModel.h"
#import "DSHTTPClient.h"


@interface DYZShowController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSArray *carShowArr;
@property (nonatomic, strong) UITableView *tableView;

@end

static NSString *newLaunchCellID = @"newLaunchCellID";

@implementation DYZShowController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self loadData];
    [self setupTableView];
    
    
}



- (void)loadData
{
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    // type = 0/1
    [param setValue:@"0" forKey:@"type"];
    
    
    [DSHTTPClient postUrlString:@"http://iosapi.itcast.cn/car/searchlist.json.php" withParam:param withSuccessBlock:^(id data) {
        //         NSLog(@"%@", data);
        NSDictionary *dict = [data objectForKey:@"data"];
        NSArray *arr = [NSArray yy_modelArrayWithClass:[DYZCarShowModel class] json:[dict objectForKey:@"carList"]];
        //        NSLog(@"%@", arr);
        self.carShowArr = arr;
        [self.tableView reloadData];
    } withFailedBlock:^(NSError *error) {
        NSLog(@"%@",error);
    } withErrorBlock:^(NSString *message) {
        NSLog(@"%@",message);
    }];
}

- (void) setupTableView
{
    UITableView *tableView = [[UITableView alloc]init];
    [tableView registerNib:[UINib nibWithNibName:@"DYZNewLaunchCell" bundle:nil] forCellReuseIdentifier:newLaunchCellID];
    //设置数据源/代理
    tableView.dataSource = self;
    tableView.delegate = self;
    //设置行高
    tableView.rowHeight = 64;
    [self.view addSubview:tableView];
    //自动布局
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.height.offset([UIScreen mainScreen].bounds.size.height-120);
    }];
    self.tableView = tableView;
    //隐藏多余的tableViewCell
    tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.carShowArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DYZNewLaunchCell *cell = [tableView dequeueReusableCellWithIdentifier:newLaunchCellID forIndexPath:indexPath];
    cell.model = self.carShowArr[indexPath.row];
    return cell;
}

//监听cell点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}


//懒加载
-(NSArray *)carShowArr
{
    if(_carShowArr == nil)
    {
        _carShowArr = [NSArray array];
    }
    return _carShowArr;
}



@end
